<template>
  <div class="expert">
    <top-header></top-header>

    <div class="search-div">
      <div class="search-one-div">
        <search-one @search="onSearch"></search-one>
      </div>

      <span class="iconfont iconshaixuan"></span>
    </div>

    <div class="common-list-body" v-fullheight>
      <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
        <van-list v-model="loading" :finished="finished" finished-text="没有更多了" @load="onLoad">
          <div class="list-item" v-for="(item, key) in listSource" :key="key" @click="goto(item)">
            <div class="name">{{ item.name }}</div>
            <div class="item">
              <span class="title">专业类别：</span>
              <span class="des">{{ item.professionCategoryName }}</span>
            </div>
            <div class="item item-1">
              <span class="title">手机号码：</span>
              <span class="des">{{ item.phonenumber }}</span>
              <a :href="`tel:${item.phonenumber}`" class="mobile-icon">
                <span class="iconfont icondianhua"></span>
              </a>
            </div>
            <div class="status" :class="`status-${item.currentState}`">
              <span>{{ expertlStatus[item.currentState] }}</span>
            </div>
            <!-- <div class="item item-1">
              <span class="title">状态：</span>
              <span class="status-span" :class="`status-span-${item.currentState}`">{{ expertlStatus[item.currentState] }}</span>
            </div> -->
          </div>
        </van-list>
      </van-pull-refresh>
    </div>
  </div>
</template>

<script lang="ts">
import { Component } from "vue-property-decorator";
import TopHeader from "@/components/TopHeader.vue";
import { List, Cell, PullRefresh } from "vant";
import SearchOne from "@/components/SearchOne.vue";
import Api from "@/api";
import Base from "@/utils/base";
import Enum from "@/typings/Enum";

@Component({
  name: "Members",
  components: {
    TopHeader,
    SearchOne,
    VanList: List,
    VanCell: Cell,
    VanPullRefresh: PullRefresh
  }
})
export default class Members extends Base {
  private listSource: any[] = [];
  private loading = false;
  private finished = false;
  private refreshing = false;

  private pageNum = 1;
  private pageSize = 20;
  private sortBy = "currentState"; // 排序字段
  private order = "asc"; // 排序顺序
  private name = "";
  private common_search_tags = "";
  private searchValue = ""; // 消息内容

  private expertlStatus = Enum.ExpertlStatus;

  private onSearch(event: any) {
    this.searchValue = event;
    this.refreshing = true;
    this.onRefresh();
  }

  private onLoad() {
    if (this.finished) {
      return;
    }
    setTimeout(async () => {
      if (this.refreshing) {
        this.pageNum = 1;
        this.refreshing = false;
      }
      const res = await Api.GetExpertList(this.listParams);
      if (res && res.code === 200) {
        if (this.pageNum === 1) {
          this.listSource = res.rows;
        } else {
          this.listSource.push(...res.rows);
        }
        if (res.total <= this.listSource.length) {
          this.finished = true;
        }
        this.pageNum++;
      } else {
        this.finished = true;
      }
      this.loading = false;
    }, 500);
  }

  private onRefresh() {
    // 清空列表数据
    this.finished = false;

    // 重新加载数据
    // 将 loading 设置为 true，表示处于加载状态
    this.loading = true;
    this.onLoad();
  }

  private goto(item: any) {
    localStorage.setItem("expertDetail", JSON.stringify(item));
    this.$router.push({ name: "ExpertDetail", query: { guid: item.guid } });
  }

  get listParams() {
    return {
      pageNum: this.pageNum,
      pageSize: this.pageSize,
      sortBy: this.sortBy,
      order: this.order,
      userId: this.currentUser.userId,
      name: this.name,
      common_search_tags: this.common_search_tags,
      searchValue: this.searchValue
    };
  }
}
</script>

<style lang="less" scoped>
.expert {
  .search-div {
    background-color: white;
    display: flex;
    align-items: center;
    .iconshaixuan {
      padding: 10px 10px 10px 0px;
      font-size: 22px;
      color: #bfbfbf;
    }
    .search-one-div {
      flex: 1;
    }
  }

  .list-item {
    margin: 10px 10px 0px 10px;
    background: #ffffff;
    border-radius: 8px;
    display: block;
    padding: 15px;
    position: relative;
    .name {
      font-size: 16px;
    }
    .item {
      color: #717787;
      margin-top: 8px;
      display: flex;

      .title {
        width: 85px;
      }
      .des {
        flex: 1;
      }
    }
    .item-1 {
      align-items: center;
    }
  }

  .icondianhua {
    color: #5e9efb;
    font-size: 25px;
  }

  .status {
    margin-left: 18px;
    font-size: 12px;
    position: absolute;
    top: 0;
    right: 0;
    span {
      display: inline-block;
      line-height: 20px;
      height: 20px;
      width: 48px;
      background: rgba(54, 207, 201, 0.14);
      color: #36cfc9;
      text-align: center;
      border-radius: 0px 8px 0px 10px;
    }
    &.status-1 span {
      color: #8b8b8b;
      background: rgba(70, 70, 70, 0.14);
    }
    &.status-2 span {
      color: #ffb700;
      background: rgba(255, 183, 43, 0.14);
    }
  }

  .status-span {
    padding: 0 6px;
    height: 18px;
    line-height: 18px;
    text-align: center;
    font-size: 12px;
    color: white;
    border-radius: 18px;
    background: #36cfc9;
  }
  .status-span-0 {
    background: #36cfc9;
  }
  .status-span-1 {
    background: #9ea9bc;
  }
}
</style>
